﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections;
using System.Xml;
using System.Web.Services;
using FileManager;
using System.Data.OleDb;

public partial class Comod_PriceInExcel : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GvData.DataBind();
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)//上传文件
    {
        if (FileUpload1.HasFile)
        {
            //try
            //{
                string path = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString()) + "xls\\";//获取程序根目录
                path += Path.GetFileName(FileUpload1.FileName);
                FileUpload1.PostedFile.SaveAs(path);
                // BindGrid(lblCurrentPath.Text);

                //string xlsPath = path; // 绝对物理路径
                string dbName=Path.GetFileName(FileUpload1.FileName).Replace(".xls","");
                // 查询语句
                string xlsPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "xls\\" + Path.GetFileName(FileUpload1.FileName);


                //导入到GridView                
                string strSource = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsPath + "; Extended Properties =Excel 8.0";
                OleDbConnection conn = new OleDbConnection(strSource);
                string sqlstring = @"SELECT * FROM [Sheet1$]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(sqlstring, conn);

                DataSet da = new DataSet();
                adapter.Fill(da);
                this.GvData.DataSource = da.Tables[0];
                this.GvData.DataBind();
                conn.Close();

                if (GvData.Rows.Count > 0) {//确定更新可用
                    btnPriceUpdata.Enabled = true;
                }
                //this.GvData.DataSource = GetExcelContent(xlsPath, dbName);
                  //  this.GvData.DataBind();

                //}
                //catch
                //{
                    //ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('文件上传失败！')", true);
                //}
        }
    }

    protected void btnPriceUpdata_Click(object sender, EventArgs e)
    {
        ZSql zs = new ZSql();
        string sql = "";
        int i = 0;
        for (i = 0; i < GvData.Rows.Count;i++ )
        {
            sql = "update CS_Stock set ";
            sql += "SuppliMoney=" + f.RoundInt(GvData.Rows[i].Cells[4].Text) + ",";//供价
            sql += "ChainMoney=" + f.RoundInt(GvData.Rows[i].Cells[5].Text) + ",";//连锁供价
            sql += "WholeMoney1=" + f.RoundInt(GvData.Rows[i].Cells[6].Text) + ",";//批发价
            sql += "Prices=" + f.RoundInt(GvData.Rows[i].Cells[7].Text) + ",";//标价
            sql += "RetailMoney=" + f.RoundInt(GvData.Rows[i].Cells[8].Text) + ",";//零售价
            sql += "RestMoney=" + f.RoundInt(GvData.Rows[i].Cells[9].Text) + ",";//最低限价
            sql += "ActiveMoney=" + f.RoundInt(GvData.Rows[i].Cells[10].Text);//活动价
            sql += " where ComodBarcode=" + GvData.Rows[i].Cells[0].Text.ToString();
            //Response.Write(sql);
            //Response.End();
            zs.Open(sql);

        }
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('更新成功！更新总记录数为：" + i.ToString() + "条。\\n\\n如果不进行操作，建议暂时关闭此窗口！');window.close();opener.location.reload();", true);

    }


 
}
